﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition.Hosting;
using Microsoft.Practices.ServiceLocation;
using System.ComponentModel.Composition;
using System.Windows;
using System.Windows.Media;
using AvalonDock;

namespace DynamicUIFramework
{
    public abstract class MefBootstrapper : Bootstrapper
    {
        public MefBootstrapper(Setting setting) : base(setting) { }

        protected AggregateCatalog AggregateCatalog { get; set; }

        protected CompositionContainer Container { get; set; }

        public override void Run(bool runWithDefaultConfiguration)
        {
            this.AggregateCatalog = this.CreateAggregateCatalog();
            this.ConfigureAggregateCatalog();

            this.Container = this.CreateContainer();
            if (this.Container == null)
            {
                throw new InvalidOperationException("NullCompositionContainerException");
            }

            this.ConfigureContainer();

            this.ConfigureServiceLocator();
            this.ConfigureRegionAdapterMappings();
            this.CustomConfigure();
         
            //var shell = this.Container.GetExportedValue<Shell>();
            //shell.Show();
            //shell.RestoreLayout();
        }

        protected override void ConfigureRegionAdapterMappings()
        {
            base.ConfigureRegionAdapterMappings();
        }

        protected virtual AggregateCatalog CreateAggregateCatalog()
        {
            return new AggregateCatalog();
        }

        protected virtual void ConfigureAggregateCatalog()
        {
            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(MefBootstrapper).Assembly));
        }

        protected virtual CompositionContainer CreateContainer()
        {
            CompositionContainer container = new CompositionContainer(this.AggregateCatalog);
            return container;
        }

        protected virtual void ConfigureContainer()
        {
            this.Container.ComposeExportedValue<IServiceLocator>(new MefServiceLocator(this.Container));
            this.Container.ComposeExportedValue<AggregateCatalog>(this.AggregateCatalog);
        }

        protected override void ConfigureServiceLocator()
        {
            IServiceLocator serviceLocator = this.Container.GetExportedValue<IServiceLocator>();
            ServiceLocator.SetLocatorProvider(() => serviceLocator);
        }

        protected virtual void CustomConfigure() { }

        protected override void SetTheme()
        {
            ThemeFactory.ChangeTheme(new Uri("/DynamicUIFramework;component/themes/generic.xaml", UriKind.RelativeOrAbsolute));
            //ResourceDictionary defaultResource = new ResourceDictionary { Source = new Uri("DynamicUIFramework;component/DesignerItem/DesignerItem.xaml", UriKind.Relative) };
            //Application.Current.Resources.MergedDictionaries.Add(defaultResource);

        }
    }
}
